<template>
  <div>
    <basic-dialog
      :visible.sync="unitVisible"
      :handleClose="handleCloseDialog"
      :title="'选择计量单位'"
      :show-footer="false"
      :width="'60%'"
    >
      <div class="form-container">
        <el-form ref="queryData" :inline="true" :model="queryData" @submit.native.prevent>
          <el-form-item label="计量单位名称:" prop="unitName">
            <el-input
              v-model="queryData.unitName"
              placeholder="请输入计量单位名称"
              @keyup.enter.native="handleQueryData(1)"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQueryData(1)"
              >查询</el-button
            >
            <el-button @click="handleResetData('queryData')">清空</el-button>
          </el-form-item>
        </el-form>
      </div>
      <BasicTable
        ref="basicTable"
        showActionCol
        :loading="tableInfo.loading"
        :columns="tableInfo.columns"
        :dataSource="tableInfo.list"
        :pagination="pagination"
        :pageSizes="pageSizes"
        :tableHeight="443"
        :showSelectCol="false"
        :actionRender="actionRender"
        @rowDblclick="handleSelectUnit"
        @pageChange="handlePageChange"
        @pageSizeChange="handlePageSizeChange"
      />
    </basic-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
import BasicDialog from '@/components/BasicDialog';
import BasicTable from '@/components/supplier/BasicTable';
import { getBtnsId } from '@/libs/btnsPermission';
import { queryUnitService } from '@api/supplier/components';
import { isResponseSuccess } from '@/pages/supplier/utils/bizUtils';

export default {
  name: 'SelectUnitDialog',
  components: {
    BasicDialog,
    BasicTable,
  },
  props: {
    unitVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      queryData: {
        unitName: '', // 计量单位名称
        unitNo: '' // 计量单位代码
      },
      pageSizes: [10, 20, 30, 40],
      pagination: {
        current: 1,
        total: 0,
        size: 10
      },
      tableInfo: {
        list: [],
        loading: false,
        columns: [
          {
            title: '计量单位代码',
            key: 'unitNo',
          },
          {
            title: '计量单位名称',
            key: 'unitName',
          },
          {
            title: '计量单位组名称',
            key: 'decimalPlaces'
          }
        ],
      }
    };
  },
  computed: {
    ...mapGetters('permissions/systemBtns', ['showBtn'])
  },
  methods: {
    // 操作列
    actionRender (h, { row }) {
      return (
        <div class="table-action">
          <el-button
            size="mini"
            type="text"
            onClick={() => this.handleSelectUnit(row)}
          > 选择
          </el-button>
        </div>
      );
    },
    // 选择计量单位
    handleSelectUnit (row) {
      this.$emit('handle-select', row);
      this.handleCloseDialog();
    },
    // 获取 表格数据
    handleQueryData (pageNum) {
      if (pageNum) {
        this.pagination.current = pageNum;
      }
      this.tableInfo.loading = true;
      sessionStorage.setItem('buttonId', getBtnsId('查询'));
      queryUnitService({ ...this.queryData, ...this.pagination }).then(res => {
        this.tableInfo.loading = false;
        if (isResponseSuccess(res)) {
          const { records, total } = res.result;
          this.tableInfo.list = records;
          this.pagination.total = total;
          this.reloadTableLayout()
        }
      }).catch(() => {
        this.tableInfo.loading = false;
      });
    },
    // 分页状态变更
    handlePageChange (pageNum) {
      this.pagination.current = pageNum;
      this.handleQueryData();
    },
    // 分页状态变更
    handlePageSizeChange (pageSize) {
      this.pagination.size = pageSize;
      this.handleQueryData(1);
    },
    // 重置查询条件
    handleResetData (formName) {
      this.$refs[formName].resetFields();
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:unitVisible', false);
    },
    // 表格DOM重新渲染
    reloadTableLayout () {
      this.$nextTick(() => {
        this.$refs.basicTable.$refs.elTable.doLayout();
      });
    },
  }
};
</script>
<style scoped>
.form-container {
  margin-bottom: 10px;
}
</style>
